package com.example.pt_progress;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

public class DBAdapter {
	public static final String DATABASE_NAME = "ptprogress.db";
	public static final String DB_TB_EXERICSE = "exericses";
	public static final String ID = "id";
	public static final String EX_NAME = "ex_name";
	public static final String EX_INSTR= "ex_instr";
	private static final int DATABASE_VERSION = 1;
	
	private static final String[] FROM_EXE= {EX_NAME};
	
	private SQLiteDatabase db;
	private final Context context;
	private DBOpenHelper dbHelper;
	private SQLiteQueryBuilder qb;
	
	
	public DBAdapter(Context _context) {
		this.context = _context;
		dbHelper = new DBOpenHelper(context, DATABASE_NAME, null,
				DATABASE_VERSION);
	}
	
	public void close() {
		db.close();
	}

	public void open() throws SQLiteException {
		try {
			db = dbHelper.getWritableDatabase();
		} catch (SQLiteException ex) {
			db = dbHelper.getReadableDatabase();
		}

		qb = new SQLiteQueryBuilder();
	}
	
	
	private static class DBOpenHelper extends SQLiteOpenHelper {
		public DBOpenHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}
		
		private static final String DATABASE_CREATE_EXERCISE = "create table "
				+ DB_TB_EXERICSE + " (" + ID
				+ " integer primary key autoincrement, " + EX_NAME
				+ " text not null, " + EX_INSTR + " text not null);";
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE_EXERCISE);
			db.execSQL("INSERT INTO exericses (ex_name) VALUES ('knee extension')");
			db.execSQL("INSERT INTO exericses (ex_name) VALUES ('band pull')");

		}
		
		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
				int _newVersion) {

			// Drop the old table.
			_db.execSQL("DROP TABLE IF EXISTS " + DB_TB_EXERICSE);

			// Create a new one.
			onCreate(_db);
		}
	}
	
	public Cursor getAllExerciseCursor() {
			return db.query(DB_TB_EXERICSE, FROM_EXE, null, null, null, null, null);
	}

}
